package co.yixiang.yshop.module.crm.dal.mysql.crmcustomer;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils;
import co.yixiang.yshop.module.crm.controller.admin.crmcustomer.vo.CrmCustomerPageReqVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcustomer.vo.CrmCustomerRespVO;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcustomer.CrmCustomerDO;
import co.yixiang.yshop.module.system.dal.dataobject.user.AdminUserDO;
import co.yixiang.yshop.module.system.dal.dataobject.roleauthdata.SysRoleAuthDataDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 客户 Mapper
 *
 * @author yshop
 */
@Mapper
public interface CrmCustomerMapper extends BaseMapperX<CrmCustomerDO> {

    /**
     * 根据当前用户ID查询有权限的客户数据
     * 通过sys_role_auth_data表验证用户对storageId的权限
     * 如果当前用户是管理员(isAdministrator == 1)，则不添加权限过滤条件
     */
    default PageResult<CrmCustomerDO> selectPageWithAuth(CrmCustomerPageReqVO reqVO, Long currentUserId, boolean isAdministrator) {
        LambdaQueryWrapperX<CrmCustomerDO> queryWrapper = new LambdaQueryWrapperX<CrmCustomerDO>()
                .likeIfPresent(CrmCustomerDO::getName, reqVO.getName())
                .likeIfPresent(CrmCustomerDO::getMobile, reqVO.getMobile())
                .eqIfPresent(CrmCustomerDO::getTelephone, reqVO.getTelephone())
                .eqIfPresent(CrmCustomerDO::getDealStatus, reqVO.getDealStatus())
                .eqIfPresent(CrmCustomerDO::getLevel, reqVO.getLevel())
                .eqIfPresent(CrmCustomerDO::getIndustry, reqVO.getIndustry())
                .eqIfPresent(CrmCustomerDO::getSource, reqVO.getSource())
                .likeIfPresent(CrmCustomerDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(CrmCustomerDO::getQq, reqVO.getQq())
                .eqIfPresent(CrmCustomerDO::getFollowStatus, reqVO.getFollowStatus())
                .likeIfPresent(CrmCustomerDO::getDemand, reqVO.getDemand())
                .likeIfPresent(CrmCustomerDO::getStalker, reqVO.getStalker())
                .likeIfPresent(CrmCustomerDO::getPositionName, reqVO.getPositionName())
                .eqIfPresent(CrmCustomerDO::getInPrice, reqVO.getInPrice())
                .eqIfPresent(CrmCustomerDO::getStorageId, reqVO.getStorageId())
                .likeIfPresent(CrmCustomerDO::getStorageCode, reqVO.getStorageCode())
                .likeIfPresent(CrmCustomerDO::getStorageName, reqVO.getStorageName())
                .betweenIfPresent(CrmCustomerDO::getNextTime, reqVO.getNextTime())
                .betweenIfPresent(CrmCustomerDO::getFollowTime, reqVO.getFollowTime())
                .betweenIfPresent(CrmCustomerDO::getCollectTime, reqVO.getCollectTime())
                .betweenIfPresent(CrmCustomerDO::getSignTime, reqVO.getSignTime())
                .betweenIfPresent(CrmCustomerDO::getReturnVisitTime, reqVO.getReturnVisitTime())
                .betweenIfPresent(CrmCustomerDO::getCreateTime, reqVO.getCreateTime());


        if (ObjectUtil.isNotNull(reqVO.getReturnValue())) {
            String sqlStr = "DATEDIFF(next_time, NOW())<=0 and next_time >='2020-01-01'";
            if (reqVO.getReturnValue() >= 1L) {
                sqlStr = "next_time >='2020-01-01' and DATEDIFF(next_time, NOW())>=0   and DATEDIFF(next_time, NOW()) <= " + reqVO.getReturnValue();
            }

            queryWrapper.apply(sqlStr);
        }

        // 如果当前用户不是管理员，则添加权限过滤条件
        if (!isAdministrator) {
            var sql = " (EXISTS( SELECT 1 FROM sys_role_auth_data WHERE user_id = " + currentUserId + " AND node_id = storage_id and deleted=0) or  stalker= " + currentUserId + ")";
            queryWrapper.apply(sql);
        }
        queryWrapper.orderByDesc(CrmCustomerDO::getId);

        return selectPage(reqVO, queryWrapper);
    }

    default List<CrmCustomerDO> getCustomerStatistics(CrmCustomerPageReqVO reqVO, Long currentUserId, boolean isAdministrator) {
        LambdaQueryWrapperX<CrmCustomerDO> queryWrapper = new LambdaQueryWrapperX<CrmCustomerDO>()
                .likeIfPresent(CrmCustomerDO::getName, reqVO.getName())
                .likeIfPresent(CrmCustomerDO::getMobile, reqVO.getMobile())
                .eqIfPresent(CrmCustomerDO::getTelephone, reqVO.getTelephone())
                .eqIfPresent(CrmCustomerDO::getDealStatus, reqVO.getDealStatus())
                .eqIfPresent(CrmCustomerDO::getLevel, reqVO.getLevel())
                .eqIfPresent(CrmCustomerDO::getIndustry, reqVO.getIndustry())
                .eqIfPresent(CrmCustomerDO::getSource, reqVO.getSource())
                .likeIfPresent(CrmCustomerDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(CrmCustomerDO::getQq, reqVO.getQq())
                .eqIfPresent(CrmCustomerDO::getFollowStatus, reqVO.getFollowStatus())
                .likeIfPresent(CrmCustomerDO::getDemand, reqVO.getDemand())
                .likeIfPresent(CrmCustomerDO::getStalker, reqVO.getStalker())
                .likeIfPresent(CrmCustomerDO::getPositionName, reqVO.getPositionName())
                .eqIfPresent(CrmCustomerDO::getInPrice, reqVO.getInPrice())
                .eqIfPresent(CrmCustomerDO::getStorageId, reqVO.getStorageId())
                .likeIfPresent(CrmCustomerDO::getStorageCode, reqVO.getStorageCode())
                .likeIfPresent(CrmCustomerDO::getStorageName, reqVO.getStorageName())
                .betweenIfPresent(CrmCustomerDO::getNextTime, reqVO.getNextTime())
                .betweenIfPresent(CrmCustomerDO::getFollowTime, reqVO.getFollowTime())
                .betweenIfPresent(CrmCustomerDO::getCollectTime, reqVO.getCollectTime())
                .betweenIfPresent(CrmCustomerDO::getSignTime, reqVO.getSignTime())
                .betweenIfPresent(CrmCustomerDO::getReturnVisitTime, reqVO.getReturnVisitTime())
                .betweenIfPresent(CrmCustomerDO::getCreateTime, reqVO.getCreateTime());

        // 如果当前用户不是管理员，则添加权限过滤条件
        if (!isAdministrator) {
            var sql = " (EXISTS( SELECT 1 FROM sys_role_auth_data WHERE user_id = " + currentUserId + " AND node_id = storage_id and deleted=0) or  stalker= " + currentUserId + ")";
            queryWrapper.apply(sql);
        }
        queryWrapper.orderByDesc(CrmCustomerDO::getId);

        List<CrmCustomerDO> crmCustomerDOS = selectList(queryWrapper);

        return crmCustomerDOS;
    }

}